package InetAddressUse.Useable;

import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 4-12
 * Created by lasia on 2017/11/29.
 */
public class PooledWebLog {
    private final static int NUM_THREADS = 4;

    public static void main(String args[]) {
        ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
        Queue<LogEntry> results = new LinkedList<LogEntry>();

        try (BufferedReader in = new BufferedReader(
                new InputStreamReader(new FileInputStream(args[0]),"UTF-8")
        )){
            for (String entry = in.readLine(); entry != null; entry = in.readLine()) {
                LookupTask task = new LookupTask(entry);
                Future<String> future = executorService.submit(task);
                LogEntry result = new LogEntry(entry, future);
                results.add(result);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class LogEntry {
    String original;
    Future<String> future;

    public LogEntry(String original, Future<String> future) {
        this.original = original;
        this.future = future;
    }
}
